var recommenditem;
var modalSourceLabel;
var modalSourceValue;
var recommendLength = 4;

recommend = function(label,name){
    $.ajax({
        url: '/post/data/ccd',
        type: 'post',
        data: {id:icd.ccd_id},
        dataType: 'json',
        async: true,
        success: function(doc){
            ccd = doc.ccd;
            var root = $("#recommend").children(".form-horizontal");
            recommendInit($(root));
            $(root).children(".recommendTitle").append('<h5>推荐信息:'+label+name+'</h5>')
            recommendGetSource(root,label,name);
            recommendGetName(root,label,name);
            recommendGetLateral(root,label,name);
            recommendGetSub(root,label,name);
        }
    });
}

recommendInit = function(root){
    $(root).children().remove();
    $(root).append("<div class='recommendTitle'></div>"
        +"<div class='recommendSource'></div>"
        +"<div class='recommendName'></div>"
        +"<div class='recommendLateral'></div>"
        +"<div class='recommendSub'></div>");
};

recommendGetSource = function(root,label,name){
    var str1,str2;
    switch(label){
        case '类':
        case '类名':
            str1 = "类";
            str2 = statusArray['class'];
            break;
        case '属性':
        case '名称':
            str1 = "属性";
            str2 =  statusArray['attribute'];
            break;
        case '关系':
            str1 = '关系';
            str2 =  statusArray['relation'];
            break;
        default: return;
    }
    $(root).children(".recommendSource").append('<hr style="margin: 0 0 5px 0;"/>');
    $(root).children(".recommendSource").append("<div><h6>源数据信息:</h6></div>")
    $(root).children(".recommendSource").append('<div class="row"><p class="span2">'+str1+': '+str2+'</p><input class="label" type="hidden" name='+str1+' value= '+str2+' ><span class="add-on remove-input" style="padding-left:0px"><i class="icon-zoom-in" title="显示详细"></i></span></div>');

}

recommendGetName = function(root,label,name){
    var icd_dir;
    var ccd_dir;

    switch(label){
        case '类':
        case '类名':
            icd_dir = icd['class'][statusArray['class']]['_value'];
            ccd_dir = ccd['class'][statusArray['class']]['_value'];
            break;
        case '属性':
        case '名称':
            icd_dir = icd['class'][statusArray['class']][statusArray['attribute']]['_value'];
            ccd_dir = ccd['class'][statusArray['class']][statusArray['attribute']]['_value'];
            label = "名称";
            break;
        case "类型": case "多重性": case "可见性": case "默认值": case "属性性质": case "约束":
        icd_dir = icd['class'][statusArray['class']][statusArray['attribute']][chToEn[label]];
        ccd_dir = ccd['class'][statusArray['class']][statusArray['attribute']][chToEn[label]];
        break;
        case '关系':
            icd_dir = icd['relation'][statusArray['relation']]['_value'];
            ccd_dir = ccd['relation'][statusArray['relation']]['_value'];
            break;
        case "关系类型":
            icd_dir = icd['relation'][statusArray['relation']];
            ccd_dir = ccd['relation'][statusArray['relation']];
            break;
        default: return;
    }
    var item = getSource(icd_dir,ccd_dir);
    var length = recommendLength<item.length ? recommendLength:item.length;
    $(root).children(".recommendName").append('<hr style="margin: 0 0 5px 0;"/>');
    $(root).children(".recommendName").append("<div><h6>本元素值推荐:</h6></div>");
    for(var i=0;i<length;i++){
        $(root).children(".recommendName").append('<div class="row"><p class="span2">'+label+': '+item[i]+'</p><input type="hidden" name='+label+' value='+item[i]+' ><span class="add-on remove-input" style="padding-left:5px"><i class="icon-retweet" title="替换"></i></span></div>');
    }
}

recommendGetLateral = function(root,label,name){
    var icd_dir;
    var ccd_dir;
    switch(label){
        case '类':
        case '类名':
            icd_dir = icd['class'];
            ccd_dir = ccd['class'];
            label = "类";
            break;
        case '属性':
        case '名称':
            icd_dir = icd['class'][statusArray['class']];
            ccd_dir = ccd['class'][statusArray['class']];
            break;
        case "类型": case "多重性": case "可见性": case "默认值": case "属性性质": case "约束":
            icd_dir = icd['class'][statusArray['class']][statusArray['attribute']];
            ccd_dir = ccd['class'][statusArray['class']][statusArray['attribute']];
            label = "属性元素";
            nameExc = true;
        break;
        case '关系':
            icd_dir = icd['relation'];
            ccd_dir = ccd['relation'];
            break;
        default: return;
    }
    var item = getSource(icd_dir,ccd_dir);
    var length = recommendLength<item.length ? recommendLength:item.length;
    $(root).children(".recommendLateral").append('<hr style="margin: 0 0 5px 0;"/>');
    $(root).children(".recommendLateral").append("<div><h6>相关元素推荐:</h6></div>");
    for(var i=0;i<length;i++){
        if(label === "属性元素") item[i] = enToCh[item[i]];
        $(root).children(".recommendLateral").append('<div class="row"><p class="span2">'+label+': '+item[i]+'</p><input type="hidden" name='+label+' value='+item[i]+' ><span class="add-on remove-input" style="padding-left:0px"><i class="icon-plus-sign" title="添加"></i></span></div>');
    }
}

recommendGetSub = function(root,label,name){
    var icd_dir;
    var ccd_dir;
    switch(label){
        case '类':
        case '类名':
            icd_dir = icd['class'][statusArray['class']];
            ccd_dir = ccd['class'][statusArray['class']];
            label = "属性";
            break;
        case '属性':
        case '名称':
            icd_dir = icd['class'][statusArray['class']][statusArray['attribute']];
            ccd_dir = ccd['class'][statusArray['class']][statusArray['attribute']];
            label = "属性元素";
            break;
        case "类型": case "多重性": case "可见性": case "默认值": case "属性性质": case "约束":
        return;
        break;
        case '关系':
            return;
            icd_dir = icd['relation'][statusArray['relation']];
            ccd_dir = ccd['relation'][statusArray['relation']];
            label = "类型关系";
            break;
        case "关系类型":
            //存在一些问题
            return;
            break;
        default: return;
    }
    var item = getSource(icd_dir,ccd_dir);
    var length = recommendLength<item.length ? recommendLength:item.length;
    $(root).children(".recommendSub").append('<hr style="margin: 0 0 5px 0;"/>');
    $(root).children(".recommendSub").append("<div><h6>子信息推荐:</h6></div>");
    for(var i=0;i<length;i++){
        if(label === "属性元素") item[i] = enToCh[item[i]];
        $(root).children(".recommendSub").append('<div class="row"><p class="span2">'+label+': '+item[i]+'</p><input type="hidden" name='+label+' value='+item[i]+' ><span class="add-on remove-input" style="padding-left:0px"><i class="icon-plus-sign" title="添加"></i></span></div>');
    }
}

//recommend
$(function(){
    $(".row.recommend-element").live("click",function(){
        recommenditem = $(this).parent().parent().parent().parent();
        var label = $(this).children(".span4").children("label").text();
        var name = $(this).children(".span4").children("p").text();
        switch(label){
            case "名称": case "类型": case "多重性": case "可见性": case "默认值": case "属性性质": case "约束":
            statusArray['attribute'] = $(this).parent().parent().parent().parent().children("input[type='hidden']").val();
            break;
        }
        if(label === "名称")  label="属性";
        recommend(label,name);
    });

    $(".row.recommend-attribute").live("click",function(){
        recommenditem = $(this).parent().parent().parent();
        var label = $(this).children(".span4").children("label").text();
        label="属性";
        var name = $(this).parent().parent().parent().children(".accordion-body").children().children().eq(0).children(".row").children(".span4").children("p").text();
        statusArray['attribute'] = $(this).parent().parent().parent().children("input[type='hidden']").val();
        recommend(label,name);
    });
});


$("#recommend .recommendSource .icon-zoom-in").live("click",function(){
    modalSourceLabel= $(this).parent().parent().children("input[type='hidden']").attr('name');
    modalSourceValue= $(this).parent().parent().children("input[type='hidden']").val();

    $('#modal-source-detail').modal('show');
});

$('#modal-source-detail').on('show', function () {
    $("#modal-source-detail").children(".modal-header").children("h5").text("源数据[详细信息]");
    $("#modal-source-detail").children(".modal-body").children("label").text(modalSourceLabel);
    $("#modal-source-detail").children(".modal-body").children("input").val(modalSourceValue);

    $("#modal-source-detail").children(".modal-body").find(".icon-refresh").trigger("click");
});

$("#recommend .recommendName .icon-retweet").live("click",function(){
    var label= $(this).parent().parent().children("input[type='hidden']").attr('name');
    var value= $(this).parent().parent().children("input[type='hidden']").val();

    switch(label){
        case '类名':
            var item = $("#icd-class-name").find("input[type='text']");
            $(item).val(value);
            $(item).parent().find(".icd-element-input-class-submit").trigger('click');
            break;
        case "名称": case "类型": case "多重性": case "可见性": case "默认值": case "属性性质": case "约束":
            var item = $(recommenditem).find(".control-sub label");
            var length=$(item).length
            for(var n= 0;n<length;n++){
                if($(item).eq(n).text() === label){
                    $(item).eq(n).parent().find("input[type='text']").val(value);
                    $(item).eq(n).parent().find(".icd-element-input-submit").trigger('click');
                    break;
                }
            }
            break;
        default: return;
    }
});
$("#recommend .recommendLateral .icon-plus-sign").live("click",function(){
    var label= $(this).parent().parent().children("input[type='hidden']").attr('name');
    var value= $(this).parent().parent().children("input[type='hidden']").val();
    if(label === "属性元素") {value=chToEn[value]};
    var dir = generateDir(label);

    $.ajax({
        url: '/post/data/icd-element',
        type: 'post',
        data: {
            statusArray:statusArray,
            type : dir.type,   //1
            process : 'add',  //2
            data: {new:value} //3
        },
        dataType: 'json',
        async: true,
        success: function(doc){
            if(doc.count != 1) return alert("创建失败，请刷新页面后重新尝试");
            icd = doc.icd ;
            //for(var key in icd.class) alert(key);
            getSourceNav();
            addElement(dir.type,value);
        }
    });
});
$("#recommend .recommendSub .icon-plus-sign").live("click",function(){
    var label= $(this).parent().parent().children("input[type='hidden']").attr('name');
    var value= $(this).parent().parent().children("input[type='hidden']").val();
    if(label === "属性元素") {value=chToEn[value]};
    var dir = generateDir(label);
    getSubItem(label);
    $.ajax({
        url: '/post/data/icd-element',
        type: 'post',
        data: {
            statusArray:statusArray,
            type : dir.type,   //1
            process : 'add',  //2
            data: {new:value} //3
        },
        dataType: 'json',
        async: true,
        success: function(doc){
            icd = doc.icd ;
            getSourceNav();
            if(doc.count != 1) return alert("创建失败，请刷新页面后重新尝试");
            addElement(dir.type,value);
        }
    });
});

generateDir = function(label){
    var dir={};
    switch(label){
        case '类':
            dir.type = 'class';
            break;
        case '类名':
            break;
        case '属性':
            dir.type = 'attribute'
        case '名称':
            break;
        case "属性元素":
            dir.type = 'attributeElem'
            break;
        case '关系':
            dir.type = 'relation';
            break;
    }
    return dir;
}

getSubItem = function(label){
    switch(label){
        case '属性':
            recommenditem = $(".span6-compact.content-detail .accordion").children().last();
            break;
        default :break;
    }
}

addElement = function(type,value){
    switch(type){
        case 'class':
            var header_class = $(".nav.nav-list.bs-docs-sidenav").children(".nav-header-relation");
            var html =  '<li name='+value+' class="class"><a href="javascript:;"><i class="icon-chevron-right "></i><i class="icon-remove-sign-right" title="点击叉号删除" style="display: none"></i>'+ value +'</a></li>';
            $(header_class).before(html);
            $(header_class).prev().trigger('click');
            break;
        case 'relation':
            var html = '<li name='+value+' class="relation"><a href="javascript:;"><i class="icon-chevron-right "></i><i class="icon-remove-sign-right" title="点击叉号删除" style="display: none"></i>'+ value +'</a></li>';
            $(".nav.nav-list.bs-docs-sidenav").append(html);
            $(".nav.nav-list.bs-docs-sidenav").children().trigger('click');
            break;
        case 'attribute':
            $.ajax({
                url: '/post/html/icd-element-attribute',
                type: 'post',
                data: {},
                success: function(html){
                    $(recommenditem).after(html);
                    icd_element_attribute_show($(recommenditem).next(),value,icd['class'][statusArray['class']][value]);
                }
            });
            break;
        case 'attributeElem':
            $.ajax({
                url: '/post/html/icd-element',
                type: 'post',
                data: {},
                success: function(html)
                {
                    var root =$(recommenditem).children(".accordion-body").children(".accordion-inner");
                    $(root).append(html);
                    icd_element_attributeElem_show($(root).children().last(),value,icd['class'][statusArray['class']][statusArray['attribute']][statusArray['attributeElem']]);
                    $(root).children().last().trigger('click');
                }
            });
            break;
    }
}